package xy.donate.android.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseActivity;
import com.hjq.base.BaseDialog;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import xy.donate.android.R;
import xy.donate.android.action.StatusAction;
import xy.donate.android.common.MyActivity;
import xy.donate.android.constant.Constant;
import xy.donate.android.db.AddressDao;
import xy.donate.android.db.AddressDb;
import xy.donate.android.entity.AddressEntity;
import xy.donate.android.helper.MmkvHelper;
import xy.donate.android.ui.dialog.MenuDialog;
import xy.donate.android.widget.HintLayout;

public class AddressActivity extends MyActivity implements StatusAction {

    private HintLayout mHintLayout;
    private AddressDb db;
    private String user;
    private List<AddressEntity> list = new ArrayList<>();
    private BaseQuickAdapter<AddressEntity, BaseViewHolder> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_manager;
    }

    @Override
    protected void initView() {
        FrameLayout frameLayout = findViewById(R.id.fl_add_address);
        frameLayout.setOnClickListener(view -> startActivity(AddNewAddressActivity.class));
        mHintLayout = findViewById(R.id.hint_layout);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        db = Room.databaseBuilder(this,AddressDb.class,"addressDB")
                .allowMainThreadQueries()
                .build();
        user = MmkvHelper.getInstance().getMmkv().getString(Constant.USER_ID_NUM,"");
        adapter = new BaseQuickAdapter<AddressEntity, BaseViewHolder>(R.layout.item_address_list) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, AddressEntity addressEntity) {
                holder.setText(R.id.tv_name,addressEntity.getRealName());
                holder.setText(R.id.tv_detail_address,addressEntity.getDetailAddress());
                holder.setText(R.id.tv_phone,addressEntity.getPhoneNum());
                holder.getView(R.id.tv_bj).setOnClickListener(view -> {
                    new MenuDialog.Builder(getActivity())
                            .setList(getResources().getStringArray(R.array.bj_array))
                            .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                                switch (position){
                                    case 0:
                                        Intent intent = new Intent(getActivity(),AddNewAddressActivity.class);
                                        intent.putExtra("address",addressEntity);
                                        startActivity(intent);
                                        break;
                                    case 1:
                                        db.addressDao().deleteLogin(addressEntity.getAddressID());
                                        toast("删除成功");
                                        break;
                                }
                            })
                            .show();
                });
            }
        };
        adapter.setOnItemClickListener((adapter, view, position) -> {
            if (getIntent().hasExtra("order")){
                Intent intent = new Intent();
                intent.putExtra("address",this.adapter.getItem(position));
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(user)){
            showLoading();
            list = db.addressDao().getAll(user);
            if (list != null && list.size() != 0){
                mHintLayout.setVisibility(View.GONE);
                showComplete();
            }else {
                showEmpty();
            }
            adapter.setNewInstance(list);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public HintLayout getHintLayout() {
        return mHintLayout;
    }
}
